<?php
declare (strict_types=1);

namespace app\business\controller;


use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\App;
use think\facade\View;
use think\Request;
use think\response\Json;

class Role extends Base
{

    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\BusinessRole';

    //关联模型
    protected $hasOne;

    //额外查询条件
    protected $extraSearch = ['business_id'];

    public function index()
    {
        $extra_map = ['business_id'=>$this->admin['business_id']];
        return $this->setTable($extra_map);
    }

    public function setTable($extra_map=[])
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'title', 'title' => '角色名', 'modify' => 'add', 'format' => 'input'])
            ->addRow(['field' => 'explain', 'title' => '角色描述', 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow(['field' => 'create_time', 'title' => '创建时间'])
            ->addRow(['field' => 'update_time', 'title' => '更新时间'])
            ->addRow(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120, 'unresize' => true])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addTopButtons(['add'])
            ->addRightButtons(['give' => [
                'title' => '分配权限',
                'type' => 'giveRole',
                'url' => url('role/give')->build()
            ], 'edit', 'delete'])
            ->setSearch($this->setSearch())
            ->addSwitchTool([['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭']])
            ->addJsUrl(['role'])
            ->addExtraMap($extra_map)
            ->fetch('role/index');
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['title', '角色名', 'input', '', '', '', 'like']);
    }


    /**
     * 原点 2020年8月5日17:16:48 分配权限
     *
     * @param Request $request -- 请求管理类
     * @param $id -- 管理员ID
     * @return string|Json
     */
    public function give($id)
    {
        if ($this->request->isAjax()) {
            try {

                $param = $this->request->param();
                $param['rules'] = $param['rules'] ? implode(',', $param['rules']) : '';
                $this->model::update($param);
                return json(['code' => 1, 'msg' => '操作成功']);

            } catch (\Exception $e) {
                return json(['code' => -1, 'msg' => $e->getMessage()]);
            }

        }
        $menu = new \app\common\model\Menu();
        View::assign('list', $menu->getBusinessAuth($id));
        return View::fetch();
    }

    public function getList($takeOver = true): Json
    {
        return parent::getList($takeOver); // TODO: Change the autogenerated stub
    }
}
